package com.example.sensors;

import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.example.singletones.contextObj;

public class Sensor_Wired_Headset extends AbstractSensor {
	public static final String NAME = "WIRED_HEADSET";
	private static boolean isConnected = false;
	public Sensor_Wired_Headset_Helper mSensor_Wired_Headset_Helper = new Sensor_Wired_Headset_Helper();

	private class Sensor_Wired_Headset_Helper extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Sensor_Wired_Headset.this.onReceive(context, intent);
		}
	}

	public Sensor_Wired_Headset() {

	}

	public void disableSensor() {
		super.disableSensor();
		contextObj.getInstance().getmContext()
				.unregisterReceiver(mSensor_Wired_Headset_Helper);
	}

	public void enableSensor() {
		super.enableSensor();
		IntentFilter receiverFilter = new IntentFilter(
				Intent.ACTION_HEADSET_PLUG);
		contextObj.getInstance().getmContext()
				.registerReceiver(mSensor_Wired_Headset_Helper, receiverFilter);
	}

	public void onReceive(Context arg0, Intent arg1) {
		boolean tisConnected = arg1.getIntExtra("state", 0) == 1;
		if (tisConnected != isConnected) {
			isConnected = tisConnected;
			String onOff;
			onOff = isConnected ? "ON" : "OFF";
			Map<String, Object> eventArgs = initEventArgs();
			eventArgs.put("eventType", "sensorInfoReceived");
			eventArgs.put("wiredHeadsetActivity", onOff);
			OnSensorChanged(eventArgs);
		}

	}

	public String getSensorName() {
		return NAME;
	}
}
